package com.xjscrm.console.web.wwCustomer;

import com.xjscrm.common.entity.CorpApplication;
import com.xjscrm.common.enums.CustomerBehaviorTypeEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.common.util.LoginContext;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.param.wwCustomer.*;
import com.xjscrm.console.service.common.corpapplication.CorpApplicationService;
import com.xjscrm.console.service.customer.wwCustomer.CustomerBehaviorService;
import com.xjscrm.console.service.customer.wwCustomer.WwCustomerService;
import com.xjscrm.console.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Api(value = "客户管理", tags = "客户管理")
@RestController
@RequestMapping(value = "/wwCustomer")
public class WwCustomerController {

    @Autowired
    private WwCustomerService wwCustomerService;
    @Autowired
    private CorpApplicationService corpApplicationService;

    @Autowired
    private CustomerBehaviorService customerBehaviorService;

    @ApiOperation(value = "查询客户总数(可筛选时间)", notes = "查询客户总数(可筛选时间)", response = Integer.class)
    @PostMapping(value = "/newTotalCustomerNumber")
    public Object newTotalCustomerNumber(@RequestBody CustomerCountDto customerCountDto) throws Exception {
        return wwCustomerService.newTotalCustomerNumber(customerCountDto);
    }

    @ApiOperation(value = "统计客户来源", notes = "统计客户来源", response = CountCustomerSceneVo.class)
    @PostMapping(value = "/countCustomerScene")
    public Object countCustomerScene(@RequestBody CustomerCountDto customerCountDto) throws Exception {
        return wwCustomerService.countCustomerScene(customerCountDto);
    }

    @ApiOperation(value = "统计性别", notes = "统计性别", response = CountCustomerGenderVo.class)
    @PostMapping(value = "/countCustomerGender")
    public Object countCustomerGender(@RequestBody CustomerCountDto customerCountDto) throws Exception {
        return wwCustomerService.countCustomerGender(customerCountDto);
    }


    @ApiOperation(value = "获取客户列表", notes = "获取客户列表", response = WwCustomerVo.class)
    @PostMapping(value = {"/getCustomer", "/getPersonCustomer"})
    public Object getCustomer(@RequestBody CustomerQuery customerQuery) throws Exception {
        return wwCustomerService.getCustomer(customerQuery);
    }

    @ApiOperation(value = "获取客户详情", notes = "获取客户详情", response = WwCustomerVo.class)
    @PostMapping(value = "/getCustomerDetail")
    public Object getCustomerDetail(@RequestBody CustomerDto customerDto) throws Exception {
        return wwCustomerService.getCustomerDetail(customerDto);
    }


    @ApiOperation(value = "修改客户信息", notes = "修改客户信息", response = boolean.class)
    @PostMapping(value = "/updateCustomer")
    public Object updateCustomer(@RequestBody CustomerDto customerDto) throws Exception {
        wwCustomerService.updateCustomer(customerDto);
        return true;
    }

    @ApiOperation(value = "获取来源下拉", notes = "获取来源下拉", response = Map.class)
    @PostMapping(value = "/getSceneDropDwon")
    public Object getSceneDropDwon(@RequestBody BaseDto baseDto) throws Exception {
        return wwCustomerService.getSceneDropDwon(baseDto);
    }

    @ApiOperation(value = "获取性别下拉", notes = "获取性别下拉", response = Map.class)
    @PostMapping(value = "/getGenderDropDwon")
    public Object getGenderDropDwon(@RequestBody BaseDto baseDto) throws Exception {
        return wwCustomerService.getGenderDropDwon(baseDto);
    }


    @ApiOperation(value = "获取客户标签树", notes = "获取客户标签树", response = Map.class)
    @PostMapping(value = "/getCustomerTagTree")
    public Object getCustomerTagTree(@RequestBody CustomerTagQuery customerTagDto) throws Exception {
        return wwCustomerService.getCustomerTagTree(customerTagDto);
    }

    @ApiOperation(value = "新增标签组(个人)", notes = "新增标签组(个人)", response = boolean.class)
    @PostMapping(value = "/addTags")
    public Object addTags(@RequestBody TagDto tagDto) throws Exception {
        wwCustomerService.addTags(tagDto);
        return true;
    }

    @ApiOperation(value = "新增客户个人标签", notes = "新增客户个人标签", response = boolean.class)
    @PostMapping(value = "/addCustomerPersonTag")
    public Object addCustomerPersonTag(@RequestBody CustomerTagDto customerTagDto) throws Exception {
        Long tagId = wwCustomerService.addCustomerPersonTag(customerTagDto);
        if (tagId != null) {
            return true;
        } else {
            return false;
        }

    }

    @ApiOperation(value = "添加客户至标签", notes = "添加客户至标签", response = boolean.class)
    @PostMapping(value = "/addCustomerToTags")
    public Object addCustomerToTags(@RequestBody AddCustomerToTagsDto addCustomerToTagsDto) throws Exception {
        wwCustomerService.addCustomerToTags(addCustomerToTagsDto);
        return true;
    }


    @ApiOperation(value = "删除客户个人标签", notes = "删除客户个人标签", response = boolean.class)
    @PostMapping(value = "/deleCustomerPersonTAgs")
    public Object deleCustomerPersonTAgs(@RequestBody DeleCustomerPersonTAgsDto deleCustomerPersonTAgsDto) throws Exception {
        wwCustomerService.deleCustomerPersonTAgs(deleCustomerPersonTAgsDto);
        return true;
    }


    @ApiOperation(value = "修改客户个人标签", notes = "修改客户个人标签", response = boolean.class)
    @PostMapping(value = "/updateCustomerPersonTAgs")
    public Object updateCustomerPersonTAgs(@RequestBody CustomerTagDto customerTagDto) throws Exception {
        wwCustomerService.updateCustomerPersonTAgs(customerTagDto);
        return true;
    }


    @ApiOperation(value = "批量修改客户备注", notes = "批量修改客户备注", response = boolean.class)
    @PostMapping(value = "/batchUpdateCustomerRemake")
    public Object batchUpdateCustomerRemake(@RequestBody BatchUpdateCustomerRemakeDto batchUpdateCustomerRemakeDto) throws Exception {
        wwCustomerService.batchUpdateCustomerRemake(batchUpdateCustomerRemakeDto);
        return true;
    }

    @ApiOperation(value = "批量打标签", notes = "批量打标签", response = boolean.class)
    @PostMapping(value = "/batchAddCustomerTag")
    public Object batchAddCustomerTag(@RequestBody BatchCustomerTagDto batchAddCustomerTagDto) throws Exception {
        wwCustomerService.batchAddCustomerTag(batchAddCustomerTagDto);
        return true;
    }

    @ApiOperation(value = "批量删除标签", notes = "批量删除标签", response = boolean.class)
    @PostMapping(value = "/batchDeleCustomerTag")
    public Object batchDeleCustomerTag(@RequestBody BatchCustomerTagDto batchCustomerTagDto) throws Exception {
        wwCustomerService.batchDeleCustomerTag(batchCustomerTagDto);
        return true;
    }


    @ApiOperation(value = "批量移动标签", notes = "批量移动标签", response = boolean.class)
    @PostMapping(value = "/batchMoveCustomerTag")
    public Object batchMoveCustomerTag(@RequestBody BatchCustomerTagDto batchCustomerTagDto) throws Exception {
        wwCustomerService.batchMoveCustomerTag(batchCustomerTagDto);
        return true;
    }


    @ApiOperation(value = "批量删除用户", notes = "批量删除用户", response = boolean.class)
    @PostMapping(value = "/batchDeleCustomer")
    public Object batchDeleCustomer(@RequestBody BatchDeleCustomerDto batchDeleCustomerDto) throws Exception {
        wwCustomerService.batchDeleCustomer(batchDeleCustomerDto);
        return true;
    }

    @ApiOperation(value = "分页获取客户行为轨迹列表", notes = "分页获取客户行为轨迹列表", response = CustomerBehaviorVo.class)
    @PostMapping(value = {"/customerBehavior/findByPage"})
    public Object findByPage(@RequestBody CustomerBehaviorQuery customerBehaviorQuery) throws Exception {
        return customerBehaviorService.findByPage(customerBehaviorQuery);
    }

    @ApiOperation(value = "获取客户行为轨迹类型列表", notes = "获取客户行为轨迹类型列表", response = CustomerBehaviorVo.class)
    @PostMapping(value = {"/customerBehavior/type/findByList"})
    public Object findCustomerBehaviorTypeList(@RequestBody BaseDto baseDto) throws Exception {
        List<Map> customerBehaviorTypeList = new ArrayList<>();
        CustomerBehaviorTypeEnum[] values = CustomerBehaviorTypeEnum.values();
        if(values != null && values.length > 0){
            for (CustomerBehaviorTypeEnum value : values) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("label", value.getDesc());
                map.put("value", value.getValue());
                customerBehaviorTypeList.add(map);
            }
        }
        return customerBehaviorTypeList;
    }


    @ApiOperation(value = "获取客户筛选页面", notes = "获取客户筛选页面", response = WwCustomerVo.class)
    @PostMapping(value = {"/getCustomerSelectPage"})
    public Object getCustomerSelectPage(@RequestBody GetCustomerSelectPageQuery customerSelectPageQuery)throws Exception{
        return wwCustomerService.getCustomerSelectPage(customerSelectPageQuery);

    }


    @ApiOperation(value = "同步客户标签", notes = "同步客户标签", response = Boolean.class)
    @PostMapping(value = "/syncCustomerTag")
    public Object syncCustomerTag(@RequestBody SyncCustomerTagDto syncCustomerTagDto)throws Exception{
        wwCustomerService.syncCustomerTag(syncCustomerTagDto);
        return true;
    }

    @ApiOperation(value = "同步客户标签V1", notes = "同步客户标签V1", response = Boolean.class)
    @PostMapping(value = "/v1/syncCustomerTag")
    public Object syncCustomerTagV1(@RequestBody SyncCustomerTagV1Dto syncCustomerTagDto) throws Exception {
        Long corpId = syncCustomerTagDto.getCorpId();
        CorpApplication corpApplication = corpApplicationService.getByCorpId(String.valueOf(corpId));
        if (Objects.isNull(corpApplication) || StringUtils.isAnyBlank(corpApplication.getAuthCorpId(), corpApplication.getPermanentCode())) {
            throw new BusinessException("当前企业未进行应用授权");
        }
        wwCustomerService.syncCustomerTagV1(syncCustomerTagDto, corpApplication);
        return true;
    }

    @ApiOperation(value = "同步标签到远程", notes = "同步标签到远程", response = Boolean.class)
    @PostMapping(value = "/v1/syncToRemote")
    public Object syncToRemote(@RequestBody SyncTagRemoteDto syncTagRemoteDto) throws Exception {
        Long corpId = syncTagRemoteDto.getCorpId();
        CorpApplication corpApplication = corpApplicationService.getByCorpId(String.valueOf(corpId));
        if (Objects.isNull(corpApplication) || StringUtils.isAnyBlank(corpApplication.getAuthCorpId(), corpApplication.getPermanentCode())) {
            throw new BusinessException("当前企业未进行应用授权");
        }
        LoginVO login = LoginContext.getLogin();
        wwCustomerService.syncToRemote(login.getMerId(), corpId, corpApplication);
        return true;
    }


    @ApiOperation(value = "批量同步客户个人标签", notes = "批量同步客户个人标签", response = Boolean.class)
    @PostMapping(value = "/batchSyncPersonTag")
    public Object batchSyncPersonTag(@RequestBody BatchSyncPersonDto batchSyncPersonDto) throws Exception {
        wwCustomerService.batchSyncPersonTag(batchSyncPersonDto);
        return true;
    }

    @ApiOperation(value = "获取同步客户个人标签任务列表", notes = "获取同步客户个人标签任务列表", response = SyncTagJobVo.class)
    @PostMapping(value = "/getBatchSyncPersonTagList")
    public Object getBatchSyncPersonTagList(@RequestBody BatchSyncPersonTagQuery batchSyncPersonTagQuery) throws Exception {
        return wwCustomerService.getBatchSyncPersonTagList(batchSyncPersonTagQuery);
    }


    @ApiOperation(value = "删除同步客户个人标签任务", notes = "删除同步客户个人标签任务", response = Boolean.class)
    @PostMapping(value = "/delSyncPersonTag")
    public Object delSyncPersonTag(@RequestBody DelSyncPersonTagDto delSyncPersonTagDto) throws Exception {
        wwCustomerService.delSyncPersonTag(delSyncPersonTagDto);
        return true;
    }


    @ApiOperation(value = "同步通讯录", notes = "同步通讯录", response = Boolean.class)
    @PostMapping(value = "/syncCustomer")
    public Object syncCustomer(@RequestBody SyncCustomerDto syncCustomerDto)throws Exception{
        wwCustomerService.syncCustomer(syncCustomerDto);
         return true;
    }


    @ApiOperation(value = "批量同步通讯录", notes = "批量同步通讯录", response = Boolean.class)
    @PostMapping(value = "/batchSyncCustomer")
    public Object batchSyncCustomer(@RequestBody BatchSyncCustomerDto batchSyncCustomerDto) throws Exception {
        wwCustomerService.batchSyncCustomer(batchSyncCustomerDto);
        return true;
    }

    @ApiOperation(value = "查询同步通讯录任务列表", notes = "查询同步通讯录任务列表", response = SyncCustomerJobVo.class)
    @PostMapping(value = "/getSyncCustomerList")
    public Object getSyncCustomerList(@RequestBody SyncCustomerQuery syncCustomerQuery) throws Exception {
        return wwCustomerService.getSyncCustomerList(syncCustomerQuery);
    }

    @ApiOperation(value = "删除同步通讯录任务", notes = "删除同步通讯录任务", response = Boolean.class)
    @PostMapping(value = "/delSyncCustomer")
    public Object delSyncCustomer(@RequestBody DelSyncCustomerDto delSyncCustomerDto) throws Exception {
        wwCustomerService.delSyncCustomer(delSyncCustomerDto);
        return true;
    }


    @ApiOperation(value = "同步个人标签状态", notes = "同步个人标签状态", response = Boolean.class)
    @PostMapping(value = "/syncPersonTagStatus")
    public Object syncPersonTag(@RequestBody SyncPersonTagDto syncCustomerDto)throws Exception{
        wwCustomerService.syncPersonTag(syncCustomerDto);
        return true;
    }


    @ApiOperation(value = "导出客户标签", notes = "导出客户标签", response = Boolean.class)
    @PostMapping(value = {"/exportCustomerTags","/exportPersonTags"})
    public void exportCustomerTags(@RequestBody CustomerQuery customerQuery, HttpServletResponse response)throws Exception{
        wwCustomerService.exportCustomerTags(customerQuery,response);
    }

    @ApiOperation(value = "获取客户所在群聊", notes = "获取客户所在群聊", response = WwGroupVo.class)
    @PostMapping(value = "/getWwGroup")
    public Object getWwGroup(@RequestBody GetWwGroupQuery getWwGroupQuery)throws Exception{
      return   wwCustomerService.getWwGroup(getWwGroupQuery);
    }
}
